#!python3
# coding: utf-8

import os, re, sys

def main():
    d,_ = os.path.split(sys.argv[0])

    fs = {}

    for csv in os.listdir(d):
        m = re.match(r'^dll_export_(.*)\.csv$', csv)
        if m is None:
            continue
        # 32, 64, xp, 98
        tp = m.group(1)
        with open(os.path.join(d, csv)) as f:
            for line in f:
                m = re.match(r'^(.+),([^,]+)$', line.strip())
                if m is None:
                    continue
                dll_name, funcname = m.groups()
                dll_name = dll_name.lower()
                fs.setdefault(funcname, {})
                fs[funcname].setdefault(dll_name, [])
                fs[funcname][dll_name].append(tp)

    with open(os.path.join(d, '..', 'apis', 'funs.txt'), 'w') as f:
        for fn in sorted(fs.keys()):
            f.write(fn + '|' + ';'.join([dll + ':' + ','.join(fs[fn][dll]) for dll in sorted(fs[fn].keys())]) + '\n')

if __name__ == "__main__":
    main()
    exit(0)
